<?php
namespace LPCISII\PHP2012Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use LPCISII\PHP2012Bundle\Entity\Utilisateur;
use LPCISII\PHP2012Bundle\Form\UtilisateurType;


class InscriptionController extends Controller {
    public function inscriptionAction() {
	$utilisateur = new Utilisateur();
	$utilisateur->setType(0);

        $formBuilder = $this->createFormBuilder($utilisateur);
	$formBuilder
	    ->add('login')
            ->add('mdp')
            ->add('email')
            ->add('nom')
            ->add('prenom')
            ->add('rue')
            ->add('ville');

	$form = $formBuilder->getForm();

        $request = $this->getRequest();

	if ($request->getMethod() == 'POST') {

		$form->bindRequest($request);

		if ($form->isValid()) {
  	            $em = $this->getDoctrine()->getEntityManager();
		    $factory = $this->container->get('security.encoder_factory');
	            $encoder = $factory->getEncoder($utilisateur);
		    $password = $encoder->encodePassword($utilisateur->getMdp(), $utilisateur->getSalt());
		    $utilisateur->setMdp($password);
		    $em->persist($utilisateur);
		    $em->flush();
		    $this->get('session')->setFlash('info', 'Inscription validée');
		   return $this->redirect($this->generateUrl('LPCISIIPHP2012Bundle_homepage'));
	        }
	}

	return $this->render('LPCISIIPHP2012Bundle:Page:inscription.html.twig',array(
		'form' => $form->createView(),
	));
    }
}
